package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.CategoryBusiness;
import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.business.WebsiteBusiness;
import com.jubao.ejb.dao.BrandDao;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.entity.Brand;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Product;
import com.jubao.ejb.entity.Store;
import com.jubao.ejb.entity.User;
import com.jubao.ejb.entity.Website;
import com.tianbing.adlottery.helper.SkinUtils;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class CategoryController extends MultiActionController {
	Logger logger = Logger.getLogger(CategoryController.class.getName());

	private String category2ProductListView;

	private String category2StoreListView;
	private String category2ServantListView;

	private String category2WebsiteListView;

	private CategoryDao categoryDao;

	private BrandDao brandDao;

	private ProductBusiness productBusiness;

	private StoreBusiness storeBusiness;
	private UserBusiness userBusiness;

	private WebsiteBusiness websiteBusiness;

	private CategoryBusiness categoryBusiness;

	private SkinUtils skinUtils;

	private Integer pageSize = 3;

	/**
	 * list products
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ModelAndView listCategory2Product(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2ProductListView);
		Map model = mv.getModel();
		// --get request parameter
		Long pdgsid = skinUtils.getParameterProductCategorySysId(request);
		Long brdsid = skinUtils.getParameterBrandSysId(request);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);

		// ----------------------get data----------------------
		List<Product> productList = productBusiness.getProductList(pdgsid, brdsid, pgno, pageSize);
		long totalCount = productBusiness.getProductCount(pdgsid, brdsid);
		Category productCategory = categoryDao.find(pdgsid);
		List<Category> childProductCategoryList = categoryDao.getChildCategoryList(pdgsid, pgno, 30, null);
		Brand brand = brandDao.find(brdsid);

		//get category primary trail
		List<Category> primaryProductCategoryTrail = null;
		if (productCategory != null) {
			primaryProductCategoryTrail = categoryDao.getPrimaryCategoryTrail(pdgsid);
		}

		// get category related brands
		List<Brand> categoryBrandList = categoryBusiness.getCategoryBrands(pdgsid);
		// -------/get data--------------------------------------------

		// prepare output data map for view-----------------------------
		model.put("productList", productList);
		model.put("brand", brand);
		model.put("productCategory", productCategory);
		model.put("childProductCategoryList", childProductCategoryList);
		model.put("primaryProductCategoryTrail", primaryProductCategoryTrail);
		model.put("categoryBrandList", categoryBrandList);

		int pageItemNum = productList==null?0:productList.size();
		String otherParam = "pdgsid="+pdgsid+"&brdsid="+brdsid;
		model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize,otherParam, model);
		
		return mv;
	}

	/**
	 * list retail stores
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listCategory2Store(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2StoreListView);
		Map model = mv.getModel();
		// --get request parameter
		Long argsid = skinUtils.getParameterAreaCategorySysId(request);
		Long bsgsid = skinUtils.getParameterBusinessCategorySysId(request);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pgsize = 30;
		// --/get request parameter

		// ----------------------get data----------------------
		List<Store> storeList = storeBusiness.getStoreList(argsid, bsgsid, pgno, pageSize);
		long totalCount = storeBusiness.getStoreCount(argsid, bsgsid);
		Category areaCategory = categoryDao.find(argsid);
		//get category primary trail
		List<Category> areaPrimaryCategoryTrail = null;
		if (areaCategory != null) {
			areaPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(argsid);
		}
		List<Category> areaChildCategoryList = categoryDao.getChildCategoryList(argsid, pgno, pgsize, null);

		Category businessCategory = categoryDao.find(bsgsid);
		//get category primary trail
		List<Category> businessPrimaryCategoryTrail = null;
		if (businessCategory != null) {
			businessPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(bsgsid);
		}
		List<Category> businessChildCategoryList = categoryDao.getChildCategoryList(bsgsid, pgno, pgsize, null);
		// -------/get data--------------------------------------------

		// prepare output data map for view-----------------------------
		model.put("storeList", storeList);
		model.put("areaCategory", areaCategory);
		model.put("areaPrimaryCategoryTrail", areaPrimaryCategoryTrail);
		model.put("areaChildCategoryList", areaChildCategoryList);

		model.put("businessCategory", businessCategory);
		model.put("businessPrimaryCategoryTrail", businessPrimaryCategoryTrail);
		model.put("businessChildCategoryList", businessChildCategoryList);

		int pageItemNum = storeList==null?0:storeList.size();
		String otherParam = "argsid="+argsid+"&bsgsid="+bsgsid;
		model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize,otherParam, model);
		return mv;
	}

	
	

	/**
	 * list retail servants
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listCategory2Servant(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2ServantListView);
		Map model = mv.getModel();
		// --get request parameter
		Long argsid = skinUtils.getParameterAreaCategorySysId(request);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pgsize = 30;
		// --/get request parameter

		// ----------------------get data----------------------
		List<User> servantList = userBusiness.getServantList(argsid, -1, pgno, pageSize);
		long totalCount = userBusiness.getServantCount(argsid, -1);
		Category areaCategory = categoryDao.find(argsid);
		//get category primary trail
		List<Category> areaPrimaryCategoryTrail = null;
		if (areaCategory != null) {
			areaPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(argsid);
		}
		List<Category> areaChildCategoryList = categoryDao.getChildCategoryList(argsid, pgno, pgsize, null);

		// -------/get data--------------------------------------------

		// prepare output data map for view-----------------------------
		model.put("servantList", servantList);
		model.put("areaCategory", areaCategory);
		model.put("areaPrimaryCategoryTrail", areaPrimaryCategoryTrail);
		model.put("areaChildCategoryList", areaChildCategoryList);

		int pageItemNum = servantList==null?0:servantList.size();
		String otherParam = "argsid="+argsid;
		model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize,otherParam, model);
		return mv;
	}
	
	/**
	 * list websites
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listCategory2Website(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2WebsiteListView);
		Map model = mv.getModel();
		// --get request parameter
		Long argsid = skinUtils.getParameterAreaCategorySysId(request);
		Long wsgsid = skinUtils.getParameterWebsiteCategorySysId(request);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pgsize = 30;
		// --/get request parameter

		// ----------------------get data----------------------
		List<Website> websiteList = websiteBusiness.getWebsiteList(argsid, wsgsid, pgno, pageSize);
		long totalCount = websiteBusiness.getWebsiteCount(argsid, wsgsid);
		Category areaCategory = categoryDao.find(argsid);
		//get category primary trail
		List<Category> areaPrimaryCategoryTrail = null;
		if (areaCategory != null) {
			areaPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(argsid);
		}
		List<Category> areaChildCategoryList = categoryDao.getChildCategoryList(argsid, pgno, pgsize, null);

		Category websiteCategory = categoryDao.find(wsgsid);
		//get category primary trail
		List<Category> websitePrimaryCategoryTrail = null;
		if (websiteCategory != null) {
			websitePrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(wsgsid);
		}
		List<Category> websiteChildCategoryList = categoryDao.getChildCategoryList(wsgsid, pgno, pgsize, null);
		// -------/get data--------------------------------------------

		// prepare output data map for view-----------------------------
		model.put("websiteList", websiteList);
		model.put("areaCategory", areaCategory);
		model.put("areaPrimaryCategoryTrail", areaPrimaryCategoryTrail);
		model.put("areaChildCategoryList", areaChildCategoryList);

		model.put("websiteCategory", websiteCategory);
		model.put("websitePrimaryCategoryTrail", websitePrimaryCategoryTrail);
		model.put("websiteChildCategoryList", websiteChildCategoryList);

		int pageItemNum = websiteList==null?0:websiteList.size();
		String otherParam = "argsid="+argsid+"&wsgsid="+wsgsid;
		model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize,otherParam, model);
		return mv;
	}

	/**
	 * @param category2ProductListView the category2ProductListView to set
	 */
	public void setCategory2ProductListView(String category2ProductListView) {
		this.category2ProductListView = category2ProductListView;
	}

	/**
	 * @param category2StoreListView the category2StoreListView to set
	 */
	public void setCategory2StoreListView(String category2StoreListView) {
		this.category2StoreListView = category2StoreListView;
	}

	/**
	 * @param categoryDao the categoryDao to set
	 */
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	/**
	 * @param productBusiness the productBusiness to set
	 */
	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

	/**
	 * @param categoryBusiness the categoryBusiness to set
	 */
	public void setCategoryBusiness(CategoryBusiness categoryBusiness) {
		this.categoryBusiness = categoryBusiness;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

	/**
	 * @param logger the logger to set
	 */
	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	/**
	 * @param brandDao the brandDao to set
	 */
	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	/**
	 * @param category2WebsiteListView the category2WebsiteListView to set
	 */
	public void setCategory2WebsiteListView(String category2WebsiteListView) {
		this.category2WebsiteListView = category2WebsiteListView;
	}

	/**
	 * @param websiteBusiness the websiteBusiness to set
	 */
	public void setWebsiteBusiness(WebsiteBusiness websiteBusiness) {
		this.websiteBusiness = websiteBusiness;
	}

	public void setCategory2ServantListView(String category2ServantListView) {
		this.category2ServantListView = category2ServantListView;
	}

	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

}
